package demo03;
import java.util.Random;
import java.util.Scanner;
/*生成随机8*8随机迷宫
 * 30%生成怪物，10%生成药水，10%生成武器铁剑，10%生成大书，20%生成守护之铠
 * 角色控制
使用wasd进行移动
P代表当前位置
出去的位置，x.size y.size 最大值
5.查看背包
6.查看血量
 */
	public class MazeGame {
	    private Maze maze;
	    private Player player;
	    private boolean gameOver;

	    public void start() {
	        maze = new Maze();
	        player = new Player();
	        gameOver = false;
	        Scanner scanner = new Scanner(System.in);

	        while (!gameOver) {
	            printMaze();
	            System.out.print("输入命令 (WASD移动, V查看属性, B查看背包, Q退出): ");
	            String input = scanner.nextLine().toLowerCase();

	            if (input.length() == 0) continue;

	            switch (input.charAt(0)) {
	                case 'w': move(-1, 0); break;
	                case 's': move(1, 0); break;
	                case 'a': move(0, -1); break;
	                case 'd': move(0, 1); break;
	                case 'v': player.showStatus(); break;
	                case 'b': player.showBackpack(); break;
	                case 'q': gameOver = true; break;
	                default: System.out.println("无效命令！");
	            }

	            // 检查是否到达终点
	            if (player.getX() == maze.getSize()-1 && player.getY() == maze.getSize()-1) {
	                System.out.println("恭喜你成功逃出迷宫！");
	                gameOver = true;
	            }
	        }
	        scanner.close();
	    }

	    private void move(int dx, int dy) {
	        int newX = player.getX() + dx;
	        int newY = player.getY() + dy;

	        if (newX < 0 || newX >= maze.getSize() || newY < 0 || newY >= maze.getSize()) {
	            System.out.println("不能越界移动！");
	            return;
	        }

	        CellType cell = maze.getCell(newX, newY);
	        if (cell == CellType.F) {
	            System.out.println("前方有障碍物，无法移动！");
	            return;
	        }

	        player.move(dx, dy);
	        handleEvent(newX, newY);
	        maze.setCell(newX, newY, CellType.EMPTY);
	    }

	    private void handleEvent(int x, int y) {
	        CellType cell = maze.getCell(x, y);
	        switch (cell) {
	            case MONSTER:
	                System.out.println("遇到了一只怪物，你轻松击败了它！");
	                break;
	            case POTION:
	                player.heal();
	                System.out.println("捡到治疗药水，生命值+10！");
	                break;
	            case IRON_SWORD:
	                player.pickIronSword();
	                System.out.println("获得铁剑，攻击力+10！");
	                break;
	            case SPELL_BOOK:
	                player.pickSpellBook();
	                System.out.println("获得法术书，法术攻击+10！");
	                break;
	            case ARMOR:
	                player.pickArmor();
	                System.out.println("获得守护之铠，护甲+20！");
	                break;
	        }
	    }

	    private void printMaze() {
	        System.out.println("\n当前迷宫：");
	        for (int i = 0; i < maze.getSize(); i++) {
	            for (int j = 0; j < maze.getSize(); j++) {
	                if (i == player.getX() && j == player.getY()) {
	                    System.out.print("P ");
	                } else {
	                    switch (maze.getCell(i, j)) {
	                        case F: System.out.print("F "); break;
	                        default: System.out.print("* ");
	                    }
	                }
	            }
	            System.out.println();
	        }
	    }

	    public static void main(String[] args) {
	        new MazeGame().start();
	    }
	}